<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/11 0011
 * Time: 9:29
 */

namespace App\Tasks;

use Hyperf\Server\ServerFactory;

use Hyperf\Utils\ApplicationContext;

class BroadcastTask
{

    public function handle($data, $nowFd, $sendFds = [])
    {
        $container = ApplicationContext::getContainer();
        $webSocketServer = $container->get(ServerFactory::class)->getServer()->getServer();
        /**
         * @var \Swoole\WebSocket\Server $webSocketServer
         */
        foreach ($sendFds as $fd) {
            if ($nowFd != $fd && $webSocketServer->exists($fd)) {
                $webSocketServer->push((int)$fd, json_encode($data));
            }
        }
    }
}
